<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">数据看板</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>共{{ total }}条记录</span>
        <el-button
          style="float: right"
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click.native="openDrawer('add')"
          >添加面经</el-button
        >
      </div>
      <el-table :data="articles" stripe border>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="stem" label="标题"></el-table-column>
        <el-table-column prop="creator" label="作者"></el-table-column>
        <el-table-column prop="likeCount" label="点赞"></el-table-column>
        <el-table-column prop="views" label="浏览数"></el-table-column>
        <el-table-column prop="createdAt" label="更新时间"></el-table-column>
        <el-table-column label="操作" align="center">
          <!-- <template #default="scope">
            {{ scope.$index }} -- {{ scope.row }}
          </template> -->
          <template #default="{ row }">
            <el-button
              type="text"
              icon="el-icon-view"
              @click.native="openDrawer('preview', row.id)"
            ></el-button>
            <el-button
              type="text"
              icon="el-icon-edit"
              @click.native="openDrawer('edit', row.id)"
            ></el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              @click.native="del(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev,pager,next,jumper"
        :total="total"
        :page-size="params.pageSize"
        :current-page="params.current"
        @current-change="changeCurrent"
      ></el-pagination>
    </el-card>

    <el-drawer
      direction="rtl"
      size="50%"
      :title="drawerTitle"
      :visible.sync="isShowDrawer"
      :before-close="handleClose"
    >
      <el-form label-width="70px" :model="form" :rules="rules" ref="form">
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quillEditor
            v-model="form.content"
            @blur="this.$refs.form.validateField('content')"
          >
            ></quillEditor
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click.native="saveAdd">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import {
  getArticleList,
  removeArticle,
  addArticle,
  getArticleDetail,
  updateArticle
} from '@/api/article'
import { quillEditor } from 'vue-quill-editor'

export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  created() {
    this.getArticleList()
  },
  data() {
    return {
      params: {
        current: 1,
        pageSize: 10
      },
      articles: [],
      total: null,
      isShowDrawer: false,
      drawerType: '',
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '不能为空', trigger: blur }],
        content: [{ required: true, message: '不能为空', trigger: blur }]
      }
    }
  },
  computed: {
    drawerTitle() {
      let title = '默认标题'
      if (this.drawerType === 'add') title = '添加面经'
      if (this.drawerType === 'preview') title = '面经'
      if (this.drawerType === 'edit') title = '修改面经'
      return title
    }
  },
  methods: {
    async getArticleList() {
      const res = await getArticleList(this.params)
      this.articles = res.data.rows
      this.total = res.data.total
      console.log(res)
    },
    changeCurrent(val) {
      console.log(val)
      this.params.current = val
      this.getArticleList()
    },
    async openDrawer(type, id) {
      // console.log(type, id)
      this.isShowDrawer = true
      this.drawerType = type
      if (type !== 'add') {
        const res = await getArticleDetail(id)
        this.form = {
          ...res.data
        }
      }
    },
    async del(id) {
      await this.$confirm('此操作将该行删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await removeArticle(id)
      this.$message.success('删除成功')
      this.getArticleList()
    },
    handleClose() {
      this.isShowDrawer = false
      this.$refs.form.resetFields()
    },
    async saveAdd() {
      try {
        await this.$refs.form.validate()
        if (this.drawerType === 'add') {
          await addArticle(this.form)
          this.$message.success('添加成功')
        }
        if (this.drawerType === 'edit') {
          const { id, content, stem } = this.form
          await updateArticle({ id, content, stem })
          this.message.success('修改成功')
        }
        this.current = 1
        this.getArticleList()
        this.handleClose()
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.box-card {
  margin-top: 15px;
  box-shadow: 0 1px 22px 0 rgb(0 0 0/10%);
}
.el-pagination {
  text-align: center;
  margin-top: 16px;
}
::v-deep .ql-editor {
  min-height: 200px;
}
::v-deep .el-drawer__body {
  padding: 20px;
}
</style>
